﻿#include <QApplication>
#include <QFileInfo>
#include <QLibraryInfo>
#include <QTranslator>
#include "confsettings.h"
#include "immainwindow.h"
#include "usersettings.h"

void runAfterApplication();

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);
    QApplication a(argc, argv);
    runAfterApplication();

    auto confSettings = ConfSettings::instance();
    if(!confSettings->appName.isEmpty())
        a.setApplicationName(confSettings->appName);
    a.setApplicationDisplayName(confSettings->getDisplayName());

    auto settings = UserSettings::instance();
    auto domain = settings->getString(UserSettings::sys_server_domain);
    confSettings->updateDomain(domain);
    confSettings->updateProxy(domain, settings->getString(UserSettings::sys_server_proxy));

    if(!confSettings->iconName.isEmpty())
    {
        QFileInfo fi(a.applicationDirPath()+'/'+confSettings->iconName);
        if(fi.exists())
            a.setWindowIcon(QIcon(fi.absoluteFilePath()));
    }
    else
    {
        QIcon icon(QLatin1Literal(":/gui/tray.png"));
        if(!icon.isNull())
            a.setWindowIcon(icon);
    }

    QFont font = a.font();
    font.setPixelSize(12);
    font.setFamily("Microsoft YaHei");
    a.setFont(font);

    QFile f(":/gui/stylesheet.css");
    if(f.open(QFile::ReadOnly | QFile::Text))
    {
        a.setStyleSheet(f.readAll());
        f.close();
    }

    QTranslator translator;
    if(!translator.load("qt_zh_CN", ":/gui/"))
        translator.load("qt_zh_CN");
    a.installTranslator(&translator);
    a.setQuitOnLastWindowClosed(false);

    IMMainWindow w;
    w.switchCentral(false);

    return a.exec();
}
